package com.yanqu.road.server.cmd.activity.xingyoushanshui;

import com.yanqu.road.entity.activity.youxing.config.XingYouConfig;
import com.yanqu.road.entity.activity.youxing.config.XingYouShanShuiRoleInfo;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.XingYouShanShuiProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.CmdApi;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.commonteam.CommonTeamMgr;
import com.yanqu.road.server.manger.activity.shanshui.XingYouShanShuiMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

@CmdApi(reqMsg = XingYouShanShuiProto.ShanShuiBuyRoleChessReq.class, respMsg = XingYouShanShuiProto.ShanShuiBuyRoleChessRsp.class)
@Cmd(code = Protocol.S_XING_YOU_SHAN_SHUI_BUY_ROLE, desc = "购买角色")
public class ShanShuiBuyRoleChessCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        XingYouConfig config = XingYouShanShuiMgr.getConfig();
        if (config == null || !ActivityMgr.activityInTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        //组队期，才可以购买
        if(!CommonTeamMgr.checkInTeamTime(config.getActivityInfo().getActivityId())){
            ret = GameErrorCode.E_COMMON_TEAM_NOT_IN_TEAM_TIME;
            return;
        }
        XingYouShanShuiProto.ShanShuiBuyRoleChessReq req = XingYouShanShuiProto.ShanShuiBuyRoleChessReq.parseFrom(packet.getBytes());
        XingYouShanShuiRoleInfo roleInfo = config.getRoleInfo(req.getRoleId());
        if(roleInfo == null || "0".equals(roleInfo.getPrice())){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }
        Property consume = new Property();
        consume.addProperty(GameConfig.GAME_MONEY_INGOTS, Integer.parseInt(roleInfo.getPrice()));
        CurrencyModule currencyModule = player.getModule(CurrencyModule.class);
        if(!currencyModule.currencyIsEnough(consume)){
            ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
            return;
        }
        player.getModule(PreReduceModule.class).preReduce(consume);
        XingYouShanShuiProto.CrossShanShuiBuyRoleChessReq.Builder crossReq = XingYouShanShuiProto.CrossShanShuiBuyRoleChessReq.newBuilder();
        crossReq.setActivityId(config.getActivityInfo().getActivityId());
        crossReq.setConsume(PropertyHelper.parsePropertyToString(consume));
        crossReq.setRoleId(req.getRoleId());

        player.sendUnionActivityGroupPacket(Protocol.C_XING_YOU_SHAN_SHUI_BUY_ROLE, crossReq, config.getActivityInfo().getActivityId());
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            player.sendPacket(Protocol.U_XING_YOU_SHAN_SHUI_BUY_ROLE, XingYouShanShuiProto.ShanShuiBuyRoleChessRsp.newBuilder().setRet(ret));
        }
    }
}
